<?php

namespace app\admin\controller\statistics;

use app\admin\model\AppForm;

trait History
{

    public function historyIndex()
    {
        $params = $this->request->param();
        if ($params['sealId']) {
            $where['seal_id'] = $params['sealId'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['name'] = ['like', '%' . $params['name'] . '%'];
        }
        $where['status'] = AppForm::$STATUS_FINISHED;
        $appFormModel = new AppForm();
        $list = $appFormModel->with(['effectiveProcessFlow.user', 'seal', 'approval', 'user'])
            ->where('company_id', $this->company->id)
            ->where($where)
            ->order('id desc')->paginate();
        $this->assign('params', $params);
        $this->assign('list', $list);
        $condition['is_del'] = array('eq', 0);
        $condition['company_id'] = $this->company->id;
        $sealAll = db('seal')->where($condition)->select();
        $this->assign('seals', $sealAll);
        return view('statistics/history/index');
    }

    public function dc7()
    {
        $params = $this->request->param();
        if ($params['sealId']) {
            $where['seal_id'] = $params['sealId'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['name'] = ['like', '%' . $params['name'] . '%'];
        }
        $where['status'] = AppForm::$STATUS_FINISHED;
        $appFormModel = new AppForm();
        $list = $appFormModel->with(['effectiveProcessFlow.user', 'seal', 'approval', 'user'])
            ->where('company_id', $this->company->id)
            ->where($where)
            ->order('id desc')->paginate();

        vendor('phpexcel/PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //exit($objPHPExcel);
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '印章名称')
            ->setCellValue('B1', '文件编号')
            ->setCellValue('C1', '审批类型')
            ->setCellValue('D1', '文件名称')
            ->setCellValue('E1', '申请单号')
            ->setCellValue('F1', '申请用章次数')
            ->setCellValue('G1', '申请人')
            ->setCellValue('H1', '状态')
            ->setCellValue('I1', '备注')
            ->setCellValue('J1', '用章时间')
            ->setCellValue('K1', '申请时间');

        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(20);


        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['seal']['name']);         //印章名称
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['seal']['number']);       //文件编号
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['approval']['type_name']);//审批类型
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['name']);                 //文件名称
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['number']);               //申请单号
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['frequency']);            //申请用章次数
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['user']['username']);     //申请人
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['status_txt']);           //状态
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['remark']);               //备注
            $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['chapter_time'] ? date('Y-m-d H:i:s', $list[$i]['chapter_time']) : '');//用章时间
            $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $list[$i]['create_time']);          //申请时间
        }
        //7.设置保存的Excel表格名称
        $filename = 'yz' . date('ymd', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('cs');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;


    }

    public function historyList($sealId)
    {
        $params = $this->request->param();
        if ($params['startTime'] && $params['endTime']) {
            $where['create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['name'] = ['like', '%' . $params['name'] . '%'];
        }
        $where['status'] = AppForm::$STATUS_FINISHED;
        $appFormModel = new AppForm();
        $list = $appFormModel->with(['effectiveProcessFlow.user', 'seal', 'approval', 'user'])
            ->where('company_id', $this->company->id)
            ->where($where)
            ->where('seal_id', $sealId)
            ->order('id desc')->paginate();
        $this->assign('params', $params);
        $this->assign('list', $list);
        return view('statistics/history/list');
    }

    public function show()
    {
        $id = request()->param('id');
        $appFormModel = new AppForm();
        $info = $appFormModel->with(['seal', 'approval', 'user', 'processFlows.user'])->company($this->company->id)
            ->where('id', $id)->find();
        $this->assign('$row', $info);
        return view('statistics/history/show');
    }

    //服务费价格
    public function seal_price()
    {
        $seal_price = C('SEAL_PRICE');
        if (!$seal_price) {
            return apireturn(2, '暂未配置');
        } else {
            return apireturn(1, '成功', $seal_price);
        }
    }
}